const SkipList = require("../template")

function testSkipListPerformance() {
  console.log(
    "1. 大量数据插入性能测试:",
    (() => {
      const sl = new SkipList()
      const dataSize = 1000
      
      const start = performance.now()
      
      // 插入大量数据
      for (let i = 0; i < dataSize; i++) {
        sl.insert(i)
      }
      
      const end = performance.now()
      const insertTime = end - start
      
      return sl.getSize() === dataSize &&
             insertTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "2. 大量数据搜索性能测试:",
    (() => {
      const sl = new SkipList()
      const dataSize = 1000
      
      // 插入数据
      for (let i = 0; i < dataSize; i++) {
        sl.insert(i)
      }
      
      const start = performance.now()
      
      // 搜索所有数据
      let found = 0
      for (let i = 0; i < dataSize; i++) {
        if (sl.search(i)) found++
      }
      
      const end = performance.now()
      const searchTime = end - start
      
      return found === dataSize &&
             searchTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "3. 大量数据删除性能测试:",
    (() => {
      const sl = new SkipList()
      const dataSize = 1000
      
      // 插入数据
      for (let i = 0; i < dataSize; i++) {
        sl.insert(i)
      }
      
      const start = performance.now()
      
      // 删除所有数据
      for (let i = 0; i < dataSize; i++) {
        sl.delete(i)
      }
      
      const end = performance.now()
      const deleteTime = end - start
      
      return sl.getSize() === 0 &&
             deleteTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "4. 随机数据操作性能测试:",
    (() => {
      const sl = new SkipList()
      const dataSize = 500
      
      // 生成随机数据
      const randomData = Array.from({ length: dataSize }, () => Math.floor(Math.random() * 10000))
      
      const start = performance.now()
      
      // 插入随机数据
      randomData.forEach(val => sl.insert(val))
      
      // 搜索随机数据
      randomData.forEach(val => sl.search(val))
      
      // 删除随机数据
      randomData.forEach(val => sl.delete(val))
      
      const end = performance.now()
      const totalTime = end - start
      
      return sl.getSize() === 0 &&
             totalTime < 2000 // 2秒内完成
    })()
  )

  console.log(
    "5. 跳表层数性能测试:",
    (() => {
      const sl = new SkipList()
      const dataSize = 1000
      
      const start = performance.now()
      
      // 插入数据并观察层数
      for (let i = 0; i < dataSize; i++) {
        sl.insert(i)
      }
      
      const end = performance.now()
      const insertTime = end - start
      const level = sl.getLevel()
      
      return sl.getSize() === dataSize &&
             level >= 0 &&
             level <= 16 && // 最大层数限制
             insertTime < 1000
    })()
  )

  console.log(
    "6. 跳表内存效率测试:",
    (() => {
      const sl = new SkipList()
      const dataSize = 1000
      
      // 插入数据
      for (let i = 0; i < dataSize; i++) {
        sl.insert(i)
      }
      
      // 验证内存使用合理
      const size = sl.getSize()
      const level = sl.getLevel()
      
      return size === dataSize &&
             level >= 0 &&
             level <= 16
    })()
  )

  console.log(
    "7. 跳表批量操作性能测试:",
    (() => {
      const sl = new SkipList()
      const batchSize = 100
      const batches = 10
      
      const start = performance.now()
      
      // 批量插入
      for (let batch = 0; batch < batches; batch++) {
        for (let i = 0; i < batchSize; i++) {
          sl.insert(batch * batchSize + i)
        }
      }
      
      const end = performance.now()
      const insertTime = end - start
      
      return sl.getSize() === batchSize * batches &&
             insertTime < 1000
    })()
  )

  console.log(
    "8. 跳表范围查询性能测试:",
    (() => {
      const sl = new SkipList()
      const dataSize = 1000
      
      // 插入数据
      for (let i = 0; i < dataSize; i++) {
        sl.insert(i)
      }
      
      const start = performance.now()
      
      // 范围查询
      const rangeValues = sl.getRangeValues(100, 200)
      
      const end = performance.now()
      const queryTime = end - start
      
      return rangeValues.length === 101 && // 100到200包含101个值
             queryTime < 100
    })()
  )

  console.log(
    "9. 跳表排名查询性能测试:",
    (() => {
      const sl = new SkipList()
      const dataSize = 1000
      
      // 插入数据
      for (let i = 0; i < dataSize; i++) {
        sl.insert(i)
      }
      
      const start = performance.now()
      
      // 排名查询
      const rank = sl.getRank(500)
      
      const end = performance.now()
      const queryTime = end - start
      
      return rank === 501 && // 排名从1开始
             queryTime < 100
    })()
  )

  console.log(
    "10. 跳表并发操作性能测试:",
    (() => {
      const sl = new SkipList()
      const dataSize = 500
      
      const start = performance.now()
      
      // 并发插入和搜索
      for (let i = 0; i < dataSize; i++) {
        sl.insert(i)
        if (i % 2 === 0) {
          sl.search(i)
        }
      }
      
      const end = performance.now()
      const totalTime = end - start
      
      return sl.getSize() === dataSize &&
             totalTime < 1000
    })()
  )

  console.log(
    "11. this上下文性能测试:",
    (() => {
      const perfObj = {
        multiplier: 2,
        processPerformance: function(values) {
          const sl = new SkipList()
          const start = performance.now()
          
          values.forEach(val => sl.insert(val))
          
          const end = performance.now()
          const time = end - start
          
          return {
            size: sl.getSize(),
            time: time,
            result: sl.getSize() * this.multiplier
          }
        }
      }
      
      const values = Array.from({ length: 100 }, (_, i) => i)
      const result = perfObj.processPerformance(values)
      
      return result.size === 100 &&
             result.result === 200 &&
             result.time < 100
    })()
  )

  console.log(
    "12. 跳表性能一致性测试:",
    (() => {
      const sl1 = new SkipList()
      const sl2 = new SkipList()
      const dataSize = 100
      
      // 相同数据插入
      for (let i = 0; i < dataSize; i++) {
        sl1.insert(i)
        sl2.insert(i)
      }
      
      // 验证性能一致性
      const size1 = sl1.getSize()
      const size2 = sl2.getSize()
      const level1 = sl1.getLevel()
      const level2 = sl2.getLevel()
      
      return size1 === size2 &&
             size1 === dataSize &&
             level1 >= 0 &&
             level2 >= 0
    })()
  )
}

testSkipListPerformance()
